package com.ttn.student.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnit;
import com.ttn.common.core.entity.business.Grade;
import com.ttn.common.core.entity.studentrecord.LearningProgressRecord;
import com.ttn.common.core.entity.studentrecord.StudentRecord;
import com.ttn.common.core.entity.studentrecord.converter.StudentRecordConverter;
import com.ttn.common.core.entity.studentrecord.query.StudentRecordQuery;
import com.ttn.common.core.entity.studentrecord.vo.StudentRecordAdminVO;
import com.ttn.common.core.entity.studentrecord.vo.StudentRecordClientVO;
import com.ttn.student.mapper.StudentRecordMapper;
import com.ttn.student.openfeign.BusinessFeignService;
import com.ttn.student.service.LearningProgressRecordService;
import com.ttn.student.service.StudentRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * 学员记录(StudentRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-01-30 15:57:40
 */
@Slf4j
@Service("studentRecordService")
public class StudentRecordServiceImpl extends ServiceImpl<StudentRecordMapper, StudentRecord> implements StudentRecordService {
    @Resource
    private StudentRecordMapper studentRecordMapper;

    @Resource
    private BusinessFeignService businessFeignService;
    @Resource
    private LearningProgressRecordService learningProgressRecordService;


    @Override
    public Result queryStudentRecordPage(StudentRecordQuery studentRecordQuery) {
        IPage<StudentRecord> studentRecordIPage = new Page<>(studentRecordQuery.getPageNum(), studentRecordQuery.getPageSize());

        LambdaQueryWrapper<StudentRecord> studentRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentRecordLambdaQueryWrapper.eq(StrUtil.isNotBlank(studentRecordQuery.getStudentNum()), StudentRecord::getStudentNum, studentRecordQuery.getStudentNum());
        studentRecordLambdaQueryWrapper.eq(StrUtil.isNotBlank(studentRecordQuery.getCellPhoneNumber()), StudentRecord::getCellPhoneNumber, studentRecordQuery.getCellPhoneNumber());
        studentRecordLambdaQueryWrapper.eq(Objects.nonNull(studentRecordQuery.getStudentType()), StudentRecord::getStudentType, studentRecordQuery.getStudentType());
        studentRecordLambdaQueryWrapper.orderByDesc(StudentRecord::getCreateTime);
        studentRecordIPage = this.page(studentRecordIPage, studentRecordLambdaQueryWrapper);
        return Result.success(StudentRecordConverter.INSTANCE.studentRecord2VOPage(studentRecordIPage));
    }

    @Override
    public Result queryStudentRecordById(Integer studentRecordId) {
        StudentRecordClientVO studentRecordClientVO = new StudentRecordClientVO();
        StudentRecord studentRecord = this.getById(studentRecordId);
        if (Objects.nonNull(studentRecord)) {
            studentRecordClientVO.setId(studentRecord.getId());
            studentRecordClientVO.setStudentNum(studentRecord.getStudentNum());
            studentRecordClientVO.setStudentType(studentRecord.getStudentType());
            if (Objects.nonNull(studentRecord.getDeadLine())) {
                studentRecordClientVO.setServicePeriod(Math.toIntExact(DateUtil.between(studentRecord.getDeadLine(), DateUtil.date(), DateUnit.DAY)));
                studentRecordClientVO.setDeadLine(studentRecord.getDeadLine());
            }
            studentRecordClientVO.setAvatarUrl(studentRecord.getAvatarUrl());
            studentRecordClientVO.setGrade(studentRecord.getGrade());
            studentRecordClientVO.setNickName(studentRecord.getNickName());
            studentRecordClientVO.setCellPhoneNumber(studentRecord.getCellPhoneNumber());
            studentRecordClientVO.setExpectGender(studentRecord.getExpectGender());
            Result<Grade> gradeResult = businessFeignService.serviceQueryGradeById(studentRecord.getGrade());
            if (gradeResult.isSuccess() && Objects.nonNull(gradeResult.getData())) {
                studentRecordClientVO.setGradeName(gradeResult.getData().getGradeName());
            }

        }
        return Result.success(studentRecordClientVO);
    }

    @Override
    public Result saveStudentRecord(StudentRecord studentRecord) {
        this.save(studentRecord);
        studentRecord.setStudentNum(StrUtil.fillBefore(String.valueOf(studentRecord.getId()), '0', 5));
        this.updateById(studentRecord);
        return Result.success(studentRecord);
    }

    @Override
    public Result queryAdminStudentRecordById(Integer studentRecordId) {
        StudentRecordAdminVO studentRecordAdminVO = new StudentRecordAdminVO();
        StudentRecord studentRecord = this.getById(studentRecordId);
        if (Objects.nonNull(studentRecord)) {
            studentRecordAdminVO.setBirthday(studentRecord.getBirthday());
            studentRecordAdminVO.setGender(studentRecord.getGender());
            studentRecordAdminVO.setCreateTime(studentRecord.getCreateTime());
            studentRecordAdminVO.setLastLogin(studentRecord.getLastLogin());
            Result<Grade> gradeResult = businessFeignService.serviceQueryGradeById(studentRecord.getGrade());
            studentRecordAdminVO.setGrade(gradeResult.getData().getGradeName());
            LambdaQueryWrapper<LearningProgressRecord> learningProgressRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
            learningProgressRecordLambdaQueryWrapper.eq(LearningProgressRecord::getStudentId, studentRecord.getId());
            learningProgressRecordLambdaQueryWrapper.orderByDesc(LearningProgressRecord::getCourseUnitId);
            learningProgressRecordLambdaQueryWrapper.last("  limit 1");
            LearningProgressRecord learningProgressRecord = learningProgressRecordService.getOne(learningProgressRecordLambdaQueryWrapper);
            if (Objects.nonNull(learningProgressRecord)) {
                Result<CourseUnit> courseUnitResult = businessFeignService.serviceQueryCourseUnitByUnitId(learningProgressRecord.getCourseUnitId());
                if (courseUnitResult.isSuccess() && Objects.nonNull(courseUnitResult.getData())) {
                    String unitTaskType = "";
                    if (courseUnitResult.getData().getUnitTaskType().equals("1")) {
                        unitTaskType = "词汇";
                    }
                    if (courseUnitResult.getData().getUnitTaskType().equals("2")) {
                        unitTaskType = "听力";
                    }
                    if (courseUnitResult.getData().getUnitTaskType().equals("3")) {
                        unitTaskType = "语法";
                    } else {
                        unitTaskType = "口语";
                    }
                    studentRecordAdminVO.setLearningProgress(gradeResult.getData().getGradeName() + " " + courseUnitResult.getData().getCourseUnitName() + " " + unitTaskType);
                }

            }

            studentRecordAdminVO.setStudentType(studentRecord.getStudentType());
            studentRecordAdminVO.setNickName(studentRecord.getNickName());
            studentRecordAdminVO.setRealName(studentRecord.getRealName());
            studentRecordAdminVO.setId(studentRecord.getId());
            studentRecordAdminVO.setStudentNum(studentRecord.getStudentNum());
            studentRecordAdminVO.setCellPhoneNumber(studentRecord.getCellPhoneNumber());
            studentRecordAdminVO.setOrder(businessFeignService.queryServiceOrderDetailsByStudentId(studentRecordId).getData());

        }
        return Result.success(studentRecordAdminVO);
    }
}
